package tech.yunland.shop.web;

import com.alibaba.fastjson.JSON;
import com.moyuer.bsf.common.Pagination;
import com.moyuer.bsf.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tech.yunland.shop.domain.model.FinanceBill;
import tech.yunland.shop.domain.service.FinanceBillService;

import java.util.HashMap;
import java.util.List;

/**
 * @author 文卡<wkwenka@gmail.com>  on 17-2-22.
 */
@RestController
@RequestMapping("/finance_bill")
public class FinanceBillController extends BaseController {

    @Autowired
    private FinanceBillService financeBillService;

    /**
     * 保存
     *
     * @param financeBill
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public void insertFinanceBill(@RequestBody FinanceBill financeBill) {

        logger.info(JSON.toJSONString(financeBill, true));

        if (financeBill.getGoods() == null) {
            throw new RuntimeException("没有商品信息");
        }

        if (financeBill.getOrgan() == null) {
            financeBill.setOrgan(primaryOrgan);
        }

        if (financeBill.getCreator() == null) {
            financeBill.setCreator(currentUser);
        }

        financeBillService.insertFinanceBill(financeBill);
    }

    /**
     * 删除财务单
     *
     * @param id
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public void deleteFinanceBill(@PathVariable String id) {
        financeBillService.deleteFinanceBill(id);
    }

    /**
     * 查询所有财务单
     *
     * @param param
     * @param direction
     * @param states
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public Pagination<HashMap<String, Object>> getBillList(@RequestParam(required = false) String param,
                                                           @RequestParam(required = false) Integer direction,
                                                           @RequestParam(required = false) List<Integer> states,
                                                           @RequestParam(required = false) Integer page,
                                                           @RequestParam(required = false) Integer rows) {
        Pagination pagination = new Pagination<FinanceBill>(page, rows);
        Integer startIdx = pagination.getStartIdx();
        pagination.setCount(financeBillService.getListSize(primaryOrganId, param, states, direction));
        pagination.setRecords(financeBillService.getFinanceBillList(primaryOrganId, param, states, direction, startIdx, rows));
        return pagination;
    }

    /**
     * 修改财务单
     *
     * @param financeBill
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public void updatafinanceBill(@RequestBody FinanceBill financeBill) {
        logger.info(JSON.toJSONString(financeBill, true));

        if (financeBill.getGoods() == null) {
            throw new RuntimeException("没有商品信息");
        }

        if (financeBill.getOrgan() == null) {
            financeBill.setOrgan(primaryOrgan);
        }

        if (financeBill.getCreator() == null) {
            financeBill.setCreator(currentUser);
        }

        financeBillService.updateFinanceBill(financeBill);
    }
}
